<template>
	<view class="">
		<NavBar title="我的营销二维码" :isShowLeftBtn="true">
		</NavBar>
	</view>
	<view class="content" :style="{minHeight:contentHeight}">
		<view class="main ">
			<view class="m_b_40" style="text-align: center;">
				<text class="f_30_0">
					我的营销二维码
				</text>
			</view>
			<view class="">
				<up-image :show-loading="true" :src="codeData.imgurl" width="518rpx" height="518rpx"></up-image>
			</view>

		</view>
		<view class="m_t_70">
			<view class="submit_btn" @click="settingQrImgBase64()">
				<text class="f_30_f">
					保存二维码
				</text>
			</view>
		</view>
	</view>
</template>
<script setup lang="ts">
	import { ref, reactive, getCurrentInstance } from "vue";
	import { onLoad, onShow } from "@dcloudio/uni-app";
	import NavBar from "@/components/navbar.vue"
	const { proxy } = getCurrentInstance()
	const contentHeight = ref('100vh')
	const statusBarHeight = ref(88)
	onLoad(() => {
		uni.getSystemInfo({
			success: function (info) {
				statusBarHeight.value = (info.statusBarHeight + 44) * 2
				contentHeight.value = `calc(100vh - ${statusBarHeight.value + 76}rpx)`
			}
		});
		getUserInfo()
	})
	const userInfo = ref({
		group_name: '普通用户'
	})
	const getUserInfo = async () => {
		let res = await proxy.$http('my.getUserInfo')
		let data = res.data
		userInfo.value = data
		getYingxiaoCode()
	}
	const codeData = ref()
	const getYingxiaoCode = async () => {
		let res = await proxy.$http('my.getYingxiaoCode', { id: userInfo.value.id })
		let data = res.data
		codeData.value = data
	}
	const settingQrImgBase64 = () => {
		uni.showLoading({
			title: '下载中...',
			mask: true
		})
		uni.downloadFile({
			url: codeData.value.downurl, // 这里是你接口地址 若要传参 直接 url拼接参数即可
			// header: {
			// 	'X-Authorization': uni.getStorageSync('session.login')['token']
			// },
			methods: 'GET',
			success: (res) => {
				console.log(res, 'res')
				var tempFilePath = res.tempFilePath; // 这里拿到后端返回的图片路径
				uni.saveImageToPhotosAlbum({  // 然后调用这个方法
					filePath: tempFilePath,
					success: (res) => {
						uni.hideLoading();
						uni.showToast({ title: '图片已保存' })
					}
				})
			},
			fail: () => {
				uni.hideLoading();
			}
		});
	}
</script>
<style lang="scss" scoped>
	.content {
		background: #F7F8FC;
		padding: 20rpx;
		padding-bottom: 64rpx;

		.main {
			width: 518rpx;
			padding: 34rpx 56rpx 42rpx 56rpx;
			background: #FFFFFF;
			box-shadow: 2rpx 7rpx 5rpx 0rpx rgba(197, 197, 197, 0.29);
			border-radius: 20rpx;
			margin: 0 auto;
			margin-top: 68rpx;
		}

		.submit_btn {
			width: 630rpx;
			height: 96rpx;
			margin: 0 auto;
			background: #FD6C25;
			border-radius: 48rpx;
			display: flex;
			align-items: center;
			justify-content: center;
		}
	}
</style>